import { post, fetch } from 'plugins/axios'
import { Dialog, QSpinnerGears } from 'quasar'

export function save ({state, commit}) {
  Dialog.create({
    title: '新增项目',
    message: '确定新增项目？',
    ok: '提交',
    cancel: '再想想'
  }).then(() => {
    state.loading.show({spinner: QSpinnerGears})
    post('/project/',
      state.fields
    ).then((response) => {
      commit('saveSuccess')
    }).catch((error) => {
      if (error.response.data) {
        commit('saveFail', error.response.data)
      }
    })
  }).catch(() => {
    console.log('stay')
  })
}

export function projectJavaopt ({state, commit}) {
  fetch('/project/get_javaopt/', {

  }).then((response) => {
    commit('projectJavaoptSuccess', response)
  }).catch((error) => {
    state.etext = error.response
  })
}

export function projectGetPubcmd ({state, commit}) {
  fetch('/project/get_pubcmd/', {
    env: state.fields.env
  }).then((response) => {
    commit('projectGetPubcmdSuccess', response)
  }).catch((error) => {
    state.etext = error.response
  })
}

export function projectGetHost ({state, commit}) {
  fetch('/project/get_host/', {
    env: state.fields.env
  }).then((response) => {
    commit('projectGetHostSuccess', response)
  }).catch((error) => {
    state.etext = error.response
  })
}

export function projectGetService ({state, commit}) {
  fetch('/project/get_service/', {
    env: state.fields.env
  }).then((response) => {
    commit('projectGetServiceSuccess', response)
  }).catch((error) => {
    state.etext = error.response
  })
}

export function getDockerCmd ({state, commit}) {
  fetch('/project/get_dockercmd/', {
    env: state.fields.env
  }).then((response) => {
    commit('getDockerCmdSuccess', response)
  }).catch((error) => {
    state.etext = error.response
  })
}

export function getDockerOpt ({state, commit}) {
  fetch('/project/get_dockeropt/', {

  }).then((response) => {
    commit('getDockerOptSuccess', response)
  }).catch((error) => {
    state.etext = error.response
  })
}

export function changePublishType ({state, dispatch, commit}) {
  commit('changePublishType')
  if (state.fields.publish_type === 1) {
    state.showVMSelect = true
    dispatch('projectJavaopt')
    if (state.fields.env) {
      dispatch('projectGetPubcmd')
    }
  } else {
    state.showVMSelect = false
  }
}

export function changeProjectEnv ({state, dispatch, commit}) {
  let fun = []
  fun.push(commit('changeProjectEnv'))
  fun.push(dispatch('projectGetHost'))
  fun.push(dispatch('projectGetService'))
  if (state.showVMSelect && state.fields.publish_type === 1) {
    fun.push(dispatch('projectGetPubcmd'))
  }
  if (state.showDockerSelect) {
    fun.push(dispatch('getDockerCmd'))
  }
  Promise.all(fun).then((result) => {
    console.log('done')
  }).catch((error) => {
    console.log(error)
  })
}

export function changePubVersion ({state, dispatch, commit}) {
  switch (state.fields.pub_version) {
    case 0:
      state.showBuildParam = false
      state.showDockerSelect = false
      state.fields.docker_opt = ''
      state.fields.docker_cmd = ''
      break
    case 1:
      state.showBuildParam = true
      state.showDockerSelect = false
      state.fields.docker_opt = ''
      state.fields.docker_cmd = ''
      break
    case 2:
      state.showBuildParam = true
      state.showDockerSelect = true
      dispatch('getDockerOpt')
      if (state.fields.env) {
        dispatch('getDockerCmd')
      }
  }
}
